//core.manage.js

jQuery('form.inline-block-edit').entwine({

    'initAjaxForm': function(pBlockId){
	container = jQuery('#' + pBlockId);

	jQuery(this).ajaxForm({
	    target: '#' + pBlockId,
 	    beforeSubmit: function(){
		container.showIndicator();
 	    },
	    success: function(){
		jQuery('#' + pBlockId).addClass('silk-editable');
		container.unblock();
	    }
	});
    }

});

jQuery('.silk-editable').entwine({

    'onmouseover': function(){
	jQuery(this).addClass('silk-core-editable-on').attr('title', 'Edit this block').children().addClass('silk-core-editable-on').attr('title', 'Edit this block');
    },

    'onmouseout': function(){
	jQuery(this).removeClass('silk-core-editable-on').children().removeClass('silk-core-editable-on');
    },

    'onclick': function(){
	var block_id = jQuery(this).attr('id');
	jQuery(this).loadEditor(block_id);
    },

    'loadEditor': function(pBlockId){
	jQuery(this).showIndicator();
	jQuery(this).removeClass('silk-editable').removeClass('silk-core-editable-on').children().removeClass('silk-core-editable-on');
	var page_slug = jQuery('body').attr('id');

	jQuery(this).load('/' + page_slug + '?action=editblock&block_id=' + pBlockId, function(){
	    jQuery('form.inline-block-edit').initAjaxForm(pBlockId);
	    jQuery(this).unblock();
	} );
    },

    'showIndicator': function(){
	jQuery(this).block({
	    message: '<h1>Loading.....</h1>'
	});
    },

});

jQuery('input.silk-slug-from').entwine({

    'onmatch': function(){

	jQuery(this).blur(function(){
	    var val = jQuery(this).val();

	    var slugTo = jQuery('input.silk-slug-to');

	    if (slugTo.length > 0){
		if (slugTo.val() == '')
		    slugTo.val(val.toLowerCase().replace(/[^a-z0-9]/, '-'));
	    }
	});
    }

});